package com.moleq.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.moleq.base.MoleQBaseDialog;
import com.moleq.base.OnResultListener;
import com.moleq.device.DeviceManager;
import com.moleq.logic.TransactionManager;
import com.moleq.logic.UIManager;
import com.moleq.mgpos.widget.NumberPad;
import com.moleq.mgpos.widget.NumberPad.NumberPadListener;
import com.moleq.mgpos.widget.NumberPadButton;
import com.moleq.models.FunctionPanel;
import com.moleq.mgpos.activities.R;
import com.moleq.util.StringUtils;

public class ItemCouponDialog extends MoleQBaseDialog {

	private Button btnConfirm;
	private Button btnClose;
	private TransactionManager mPaymentMgr;
	private NumberPad mNumPad;
	private EditText et_coupon_value;
	private DeviceManager mDeviceManager;
	private String type = "A";
	private TextView txt_coupon_value;
	
	private Button.OnClickListener mOnClickListener = new Button.OnClickListener() {

		@Override
		public void onClick(View arg0) {
			switch (arg0.getId()) {
			case R.id.buttonItemCouponClose:
				onCloseWithReturn("");
				break;
			case R.id.buttonItemCouponConfirm:
				mDeviceManager.enableAllScanner();
				String strAuth = et_coupon_value.getText().toString();
				
				if (strAuth != null && !strAuth.equals("") && !strAuth.equals(".")) {
					double dValue = Double.valueOf(strAuth);
					if (type.equals(FunctionPanel.COUPON_MANUAL_AMOUNT)) {
						dValue = StringUtils.toDouble(strAuth);
					}
					if (type.equals(FunctionPanel.COUPON_MANUAL_PERCENT) && dValue > 100) {
						MessageBox.show(mContext, mContext.getResources().getString(R.string.msg_not_more_hundred_percent));
						return;
					}
					
					onReturnValue(R.id.buttonItemCouponConfirm,dValue);
					dismiss();
				}
				
				onCloseWithReturn(arg0.getId(), strAuth);
				
				break;
			}
		}

	};
	
	public ItemCouponDialog(Context context) {
		super(context,context);
		mContext = context;
	}
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		this.setContentView(R.layout.dialog_perview_item_coupon);
		this.setCancelable(false);
		
		mDeviceManager = DeviceManager.getInstance();
		mPaymentMgr = TransactionManager.getInstance();
		
		btnConfirm = (Button) findViewById(R.id.buttonItemCouponConfirm);
		btnConfirm.setOnClickListener(mOnClickListener);

		btnClose = (Button) findViewById(R.id.buttonItemCouponClose);
		btnClose.setOnClickListener(mOnClickListener);
		
		txt_coupon_value = (TextView) findViewById(R.id.txt_coupon_value);
				
		et_coupon_value = (EditText) this.findViewById(R.id.et_coupon_value);
		
		mNumPad = (NumberPad) findViewById(R.id.numberPadItemCoupon);
		mNumPad.setStyle(NumberPad.NO_ENTER_STYLE);
		mNumPad.setNumberPadListener(numberPadListener);
		
	}
	
	private NumberPadListener numberPadListener = new NumberPadListener(){

		@Override
		public void onClick(NumberPadButton button) {
			// TODO Auto-generated method stub
			switch (button) {
			case ENTER:
			
				break;
			}
		}
		
	};
	
	
	@Override
	public void show() {
		super.show();
		
		mNumPad.reset();
		mNumPad.requestFocusFromTouch();
		mNumPad.setInputEditText(et_coupon_value);
		
		et_coupon_value.setText("");
		UIManager.setFoucesByControl(et_coupon_value);
		
		if (type.equals(FunctionPanel.COUPON_MANUAL_PERCENT)) {
			txt_coupon_value.setText("Percent(%)");
		}else if (type.equals(FunctionPanel.COUPON_MANUAL_AMOUNT)) {
			txt_coupon_value.setText("Amount");
		}
		
	}


	public void setMode(String couponManualAmount) {
		type = couponManualAmount;
	}
	
}
